<?php

namespace backend\modules\business\models;

use arogachev\excel\import\basic\Importer;
use common\components\Upload;
use common\models\business\BusinessPig;
use common\models\business\BusinessPigFile;
use common\models\business\BusinessVariety;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

/**
 * BusinessPigSearch represents the model behind the search form about `common\models\business\BusinessPig`.
 */
class BusinessPigImport extends BusinessPig
{
    public $excel;

    public function import($excel)
    {
        $importer = new Importer([
//            'filePath' => \Yii::getAlias('@webroot/uploads/2bin/ceshi.xlsx'),
            'filePath' => Upload::checkGetPath($excel),
            'standardModelsConfig' => [
                [
                        'className' => parent::className(),
                        'standardAttributesConfig' => [
                            ['name' => 'no'],
                            ['name' => 'weight'],
                            ['name' => 'height'],
                            [
                                'name' => 'varietyid',
                                'valueReplacement' => function($value){
                                    return BusinessVariety::find()->select('id')->where(['title' => $value]);
                                }
                            ],
                            [
                                'name' => 'fileid',
                                'valueReplacement' => function($value){
                                    return BusinessPigFile::find()->select('id')->where(['no' => $value]);
                                }
                            ],
                            ['name' => 'batch'],
                            [
                                'name' => 'beforevaccinetime',
                                'valueReplacement' => function($value){
                                    return $value;
                                }
                            ],
                            [
                                'name' => 'aftervaccinetime',
                                'valueReplacement' => function($value){
                                    return $value;
                                }
                            ],
                            [
                                'name' => 'intime',
                                'valueReplacement' => function($value){
                                    return $value;
                                }
                            ],
                            [
                                'name' => 'planout',
                                'valueReplacement' => function($value){
                                    return $value;
                                }
                            ],
                            [
                                'name' => 'price',
                                'valueReplacement' => function($value){
                                    return $value;
                                }
                            ],
                            [
                                'name' => 'status',
                                'valueReplacement' => function($value){
                                    $newstatus = array_flip(BusinessPig::$statues);
                                    if(!array_key_exists($value,$newstatus)){
                                        return false;
                                    }

                                    return $newstatus[$value];
                                }
                            ],
                            ['name' => 'gender'],
                    ],
                ],
            ],
        ]);

        if (!$importer->run()) {
            if ($importer->wrongModel) {
                return Html::errorSummary($importer->wrongModel);

            }
        }
        Upload::deleteImg($excel);
        return true;
    }

}
